﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace chessLogic
{
    public class PGNImportParser
    {
        private string filePath;
        private List<Game> games;
        public List<Game> Games 
        {
            get { return this.games; }
            set { this.games = value; }
        }

        public PGNImportParser(string _filePath)
        {
            filePath = _filePath;
            games = new List<Game>();
        }
        public string ReadFile()
        {
            string pgnString = string.Empty;
            using (StreamReader reader = new StreamReader(filePath))
            {
                pgnString = reader.ReadToEnd();
            }
            return pgnString;
        }
        public void Parse()
        {
            string[] parts = GetPartsPGNString(ReadFile());
            for (int i = 0; i < parts.Length; i++)
            { 
                parts[i] = parts[i].Trim();
                if (parts[i][0] == '[')
                {
                    Game game = new Game();
                    game.setTags(GetTags(parts[i]));
                    if (i + 1 < parts.Length)
                    {
                        Board board = new Board();
                        game.Moves = GetMoves(parts[i + 1]);
                        games.Add(game);
                    }
                }
            }
        }
        public string[] GetPartsPGNString(string pgnString)
        {
            return pgnString.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        }
        public Dictionary<string, string> GetTags(string tagString)
        {
            Dictionary<string, string> tags = new Dictionary<string, string>();
            string[] lines = tagString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
                if (line.Contains("[") && line.Contains("]"))
                {
                    string[] parts = line.Split(new char[] { '\"' });
                    tags.Add(parts[0].Remove(0, 1).Trim(), parts[1].Trim());
                }
            return tags;
        }
        public List<string> GetMoves(string moveString)
        {
            List<string> listMoves = new List<string>();
            string[] results = { "1-0", "0-1", "1/2-1/2", "*" };

            moveString = moveString.Replace("\r\n", " ");
            string[] moveParts = moveString.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 1; i < moveParts.Length; i++)
            {
                foreach (string result in results)
                    if (moveParts[i].Contains(result))
                        moveParts[i] = moveParts[i].Replace(result, "");

                string[] move = moveParts[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                listMoves.Add(move[0]);

                if (move.Length > 1)
                    listMoves.Add(move[1]);
            }
            return listMoves;
        }
    }
}
